package com.shenyi.shop.service;

import com.shenyi.shop.api.ICategoryService;
import com.shenyi.shop.dao.CategoryMapper;
import com.shenyi.shop.entity.CategoryDO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;

/**
 * @author richard.fan
 * @version 1.0
 * @date 2018/11/05
 * @desc 分类
 * @see
 * @since 1.0
 */
@Service
public class CategoryServiceImpl implements ICategoryService {


    @Autowired
    private CategoryMapper categoryMapper;

    @Override
    public CategoryDO queryObject(Integer id) {
        return categoryMapper.queryObject(id);
    }

    @Override
    public List<CategoryDO> queryList(Map<String, Object> map) {
        return categoryMapper.queryList(map);
    }

    @Override
    public int queryTotal(Map<String, Object> map) {
        return categoryMapper.queryTotal(map);
    }

    @Override
    public int save(CategoryDO category) {
        if ("L1".equals(category.getLevel())) {
            category.setParentId(0);
        }
        return categoryMapper.save(category);
    }

    @Override
    public int update(CategoryDO category) {
        if ("L1".equals(category.getLevel())) {
            category.setParentId(0);
        }
        return categoryMapper.update(category);
    }

    @Override
    public int delete(Integer id) {
        return categoryMapper.delete(id);
    }

    @Override
    @Transactional
    public int deleteBatch(Integer[] ids) {
        categoryMapper.deleteByParentBatch(ids);
        return categoryMapper.deleteBatch(ids);
    }
}
